#!/bin/bash

set -euxo pipefail

export ARCH=x86_64

mkdir -p {{app_name}}.AppDir
cp -r ../deb/{{bundle_name}}/data/usr {{app_name}}.AppDir

cd {{app_name}}.AppDir

wget -q -O AppRun https://github.com/AppImage/AppImageKit/releases/download/continuous/AppRun-x86_64 || wget -q -O AppRun https://github.com/AppImage/AppImageKit/releases/download/12/AppRun-aarch64
chmod +x AppRun

cp usr/share/icons/hicolor/256x256/apps/{{app_name}}.png {{app_name}}.png

cd ..

echo '[Desktop Entry]' > {{app_name}}.desktop
echo 'Version=1.0' >> {{app_name}}.desktop
echo 'Comment=A Tauri App' >> {{app_name}}.desktop
echo 'Exec={{app_name}}' >> {{app_name}}.desktop
echo 'Icon={{app_name}}' >> {{app_name}}.desktop
echo 'Name={{app_name_uppercase}}' >> {{app_name}}.desktop
echo 'Terminal=false' >> {{app_name}}.desktop
echo 'Type=Application' >> {{app_name}}.desktop
echo 'Categories=X-Web;' >> {{app_name}}.desktop

cp {{app_name}}.desktop {{app_name}}.AppDir/usr/share/applications/{{app_name}}.desktop
mv {{app_name}}.desktop {{app_name}}.AppDir/{{app_name}}.desktop

mksquashfs {{app_name}}.AppDir {{app_name}}.squashfs -root-owned -noappend

wget -q -O appimagetool https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-x86_64.AppImage || wget -q -O appimagetool https://github.com/AppImage/AppImageKit/releases/download/12/appimagetool-x86_64.AppImage
chmod +x appimagetool
./appimagetool {{app_name}}.AppDir {{app_name}}.AppImage
